package com.hmall.controller;

import com.hdmall.business.common.annotation.LoginRequired;
import com.hdmall.business.common.common.ServerResponse;
import com.hmall.model.SysUser;
import com.hmall.service.CommentService;
import com.hmall.util.TokenUtil;
import com.hmall.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.JedisPool;

@RestController
@RequestMapping("/comment/")
@Api(value = "评论", description = "评论类API说明")
public class CommentController{


    private final CommentService iCommentService;

    private final JedisPool jedisPool;

    TokenUtil tokenUtil = new TokenUtil();

    @Autowired
    public CommentController(CommentService iCommentService, JedisPool jedisPool) {
        this.iCommentService = iCommentService;
        this.jedisPool = jedisPool;
    }


    /**
     * @Date 2018/11/9 16:06
     * @Param [token, productId]
     * @return com.hmall.common.ServerResponse<com.hmall.vo.MallCommentTitle>
     * @Description // 评价筛选条件
     */
    @LoginRequired
    @GetMapping("listFilter")
    @ApiOperation(value = "列出评价筛选条件")
    public ServerResponse<MallCommentTitle> listFilter(String token, Integer productId){

        return iCommentService.listFilter(productId);
    }

    /**
     * @Date 2018/11/9 16:08
     * @Param [token, type, productId, pageNum, pageSize]
     * @return com.hmall.common.ServerResponse<com.hmall.vo.PageVo>
     * @Description // 评价列表
     */
    @LoginRequired
    @GetMapping("listComment")
    @ApiOperation(value = "评价列表")
    public ServerResponse<PageVo<MallCommentVo>> listComment(String token, String type, Integer productId, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                             @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) throws Exception {
        
        SysUser user = tokenUtil.tokenToUser(jedisPool, token).getData();
        return iCommentService.listComment(user.getUserId(), productId, type, pageNum, pageSize);
    }

    /**
     * @Date 2018/11/9 16:08
     * @Param [token, order_id]
     * @return com.hmall.common.ServerResponse<com.hmall.vo.MallCommentOrderVo>
     * @Description //
     *     查询评价信息，以订单为单位
     *     根据order_id去找到是否有进行评价
     *     有的话要回显给前台
     */
    @LoginRequired
    @GetMapping("getComment")
    @ApiOperation(value = "查询出原有的评论", notes = "如果已经进行评论,不能评论，否则返回一个默认值，为评论的更新提供数据")
    public ServerResponse<MallCommentOrderVo> getComment(String token, Integer order_id){


        ServerResponse response =  iCommentService.getComment(order_id);
        System.out.println(response.getData());
        return response;
    }


    /**
     * @Date 2018/11/9 16:08
     * @Param [token, mallCommentOrderVo]
     * @return com.hmall.common.ServerResponse
     * @Description //
     * 更新评价，以订单为单位
     *     当交易完成的时候会设置一个默认的评价
     *     可以修改一次 改了之后不能进行修改
     */
    @LoginRequired
    @PostMapping("updateComment")
    @ApiOperation(value = "更新评论")
    public ServerResponse updateComment(String token, MallCommentOrderVo mallCommentOrderVo){

        return iCommentService.updateComment(mallCommentOrderVo);
    }


    /**
     * @Date 2018/11/9 16:09
     * @Param [token, commentId]
     * @return com.hmall.common.ServerResponse<com.hmall.vo.MallCommentDetailVo>
     * @Description // 评价详情页基础数据
     */
    @LoginRequired
    @GetMapping("listBasicCommentDetail")
    @ApiOperation(value = "评论详情页基础数据列表", notes = "评论详情页的基础信息，包括前一个页面的评论以及商品信息")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "token", value = "登录令牌", dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "commentId", value = "评论的主键", dataType = "Integer", paramType = "query")
    })
    public ServerResponse<MallCommentDetailVo> listBasicCommentDetail(String token, Integer commentId) throws Exception {

        
        SysUser user = tokenUtil.tokenToUser(jedisPool, token).getData();
        return iCommentService.listBasicCommentDetail(user.getUserId(), commentId);
    }

    /**
     * @Date 2018/11/9 16:09
     * @Param [token, commentId, pageNum, pageSize]
     * @return com.hmall.common.ServerResponse<com.hmall.vo.PageVo>
     * @Description // 评价详情也回复信息
     */
    @LoginRequired
    @GetMapping("listAdvancedCommentDetail")
    @ApiOperation(value = "评论详情页回复列表", notes = "评论详情页的回复信息，进行分页操作")
    public ServerResponse<PageVo<MallCommentReplyDetail>> listAdvancedCommentDetail(String token, Integer commentId, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                                                    @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){

        return iCommentService.listAdvancedCommentDetail(commentId, pageNum, pageSize);
    }




    /**
     * @Date 2018/11/9 16:09
     * @Param [token, commentId, evaluate, anoymous, replyName, replyId]
     * @return com.hmall.common.ServerResponse
     * @Description // 进行回复
     */
    @LoginRequired
    @PostMapping("replyComment")
    @ApiOperation(value = "回复评论")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "登录令牌", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "commentId", value = "评论的主键", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "evaluate", value = "回复的评论内容", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "anoymous", value = "回复人是否匿名 0不匿名 1匿名", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "replyName", value = "被回复人的昵称",dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "replyId", value = "被回复人的主键", dataType = "Integer", paramType = "query")
    })
    public ServerResponse replyComment(String token, Integer commentId, String evaluate, Integer anoymous, String replyName, Integer replyId) throws Exception {

        
        SysUser user = tokenUtil.tokenToUser(jedisPool, token).getData();
        return iCommentService.replyComment(user.getUserId(), commentId, evaluate, anoymous, replyName, replyId);

    }

    /**
     * @Date 2018/11/9 16:09
     * @Param [orderNo]
     * @return com.hmall.common.ServerResponse
     * @Description // 根据订单id生成默认评价
     */
    @GetMapping("createDefaultComment")
    @ApiOperation(value = "根据订单号生成默认评价", notes = "临时接口")
    public ServerResponse createDefaultComment(Long orderNo){

        return iCommentService.createDefaultComment(orderNo);
    }

    /**
     * @Date 2018/11/9 16:09
     * @Param [token, commentId]
     * @return com.hmall.common.ServerResponse
     * @Description // 进行点赞
     */
    @LoginRequired
    @PostMapping("doStar")
    @ApiOperation(value = "进行点赞")
    public ServerResponse doStar(String token, Integer commentId) throws Exception {

        
        SysUser user = tokenUtil.tokenToUser(jedisPool, token).getData();
        return iCommentService.doStar(user.getUserId(),commentId);
    }

    /**
     * @Date 2018/11/9 16:09
     * @Param [token]
     * @return com.hmall.common.ServerResponse
     * @Description // 待评价订单列表
     */
    @LoginRequired
    @PostMapping("needComment")
    @ApiOperation(value = "待评价订单列表")
    public ServerResponse needComment(String token) throws Exception {

        
        SysUser user = tokenUtil.tokenToUser(jedisPool, token).getData();
        return iCommentService.needComment(user.getUserId());
    }


}